package com.happy.manage.controller;

import java.util.List;

import com.happy.manage.domain.SvideoWithUser;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.happy.common.annotation.Log;
import com.happy.common.core.controller.BaseController;
import com.happy.common.core.domain.AjaxResult;
import com.happy.common.enums.BusinessType;
import com.happy.manage.domain.Svideo;
import com.happy.manage.service.ISvideoService;
import com.happy.common.utils.poi.ExcelUtil;
import com.happy.common.core.page.TableDataInfo;

/**
 * 短视频管理Controller
 * 
 * @author 艾头条
 * @date 2024-11-16
 */
@RestController
@RequestMapping("/manage/svideo")
public class SvideoController extends BaseController
{
    @Autowired
    private ISvideoService svideoService;

    /**
     * 查询短视频管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:svideo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Svideo svideo)
    {
        startPage();
        // 如果 categoryCode 为 "13"，则设置标志并重置 categoryCode
        if ("13".equals(svideo.getCategoryCode())) {
            svideo.setCategoryCode13(true);
            svideo.setCategoryCode("");
        }
        List<SvideoWithUser> list = svideoService.getSvideoWithUser(svideo);
        return getDataTable(list);
    }

    /**
     * 导出短视频管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:svideo:export')")
    @Log(title = "短视频管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Svideo svideo)
    {
        List<Svideo> list = svideoService.selectSvideoList(svideo);
        ExcelUtil<Svideo> util = new ExcelUtil<Svideo>(Svideo.class);
        util.exportExcel(response, list, "短视频管理数据");
    }

    /**
     * 获取短视频管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:svideo:query')")
    @GetMapping(value = "/{videoId}")
    public AjaxResult getInfo(@PathVariable("videoId") Long videoId)
    {
        return success(svideoService.selectSvideoByVideoId(videoId));
    }

    /**
     * 新增短视频管理
     */
    @PreAuthorize("@ss.hasPermi('manage:svideo:add')")
    @Log(title = "短视频管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Svideo svideo)
    {
        return toAjax(svideoService.insertSvideo(svideo));
    }

    /**
     * 修改短视频管理
     */
    @PreAuthorize("@ss.hasPermi('manage:svideo:edit')")
    @Log(title = "短视频管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Svideo svideo)
    {
        return toAjax(svideoService.updateSvideo(svideo));
    }

    /**
     * 删除短视频管理
     */
    @PreAuthorize("@ss.hasPermi('manage:svideo:remove')")
    @Log(title = "短视频管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{videoIds}")
    public AjaxResult remove(@PathVariable Long[] videoIds)
    {
        return toAjax(svideoService.deleteSvideoByVideoIds(videoIds));
    }

    /**
     * 更新视频观看次数
     */
    @PreAuthorize("@ss.hasPermi('manage:svideo:edit')")
    @Log(title = "短视频管理", businessType = BusinessType.UPDATE)
    @PutMapping("/viewCount/{videoId}")
    public AjaxResult incrementViewCount(@PathVariable("videoId") Long videoId)
    {
        return toAjax(svideoService.incrementViewCount(videoId));
    }

    /**
     * 获取关注用户的视频列表
     */
    @PreAuthorize("@ss.hasPermi('manage:svideo:list')")
    @GetMapping("/following/{userId}")
    public TableDataInfo followingVideos(@PathVariable("userId") Long userId)
    {
        startPage();
        List<SvideoWithUser> list = svideoService.selectFollowingUserVideos(userId);
        return getDataTable(list);
    }
}
